package com.woniuxy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.controller.form.ProjectIdForm;
import com.woniuxy.controller.form.ProjectNewroleForm;
import com.woniuxy.controller.form.ProjectroleStateForm;
import com.woniuxy.controller.info.ProjectroleInfo;
import com.woniuxy.model.ProjectRole;
import com.woniuxy.service.ProjectRoleService;
import com.woniuxy.utils.MyException;
import com.woniuxy.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 马世亮
 * @since 2022-02-15
 */
@RestController
@RequestMapping("/projectRole")
@Api(tags = "项目角色接口")
public class ProjectRoleController {

    @Resource
    public ProjectRoleService projectRoleService;

    /**
     * 新增项目角色
     * @param projectNewroleForm 项目角色信息
     * @return
     * @throws MyException
     */
    @ApiOperation("新增项目角色")
    @PostMapping("/addprojectrole")
    public Result addProjectrole(@RequestBody ProjectNewroleForm projectNewroleForm) throws MyException {
        System.out.println(projectNewroleForm);

        boolean b = projectRoleService.addprojectrole(projectNewroleForm);
        if(!b) return Result.fail(500,"新增失败，请稍后重试");
       return Result.success(200,"新增成功",null);
    }

    /**
     * 查找有效的项目角色
     * @return
     */
    @ApiOperation("查找有效项目角色")
    @PostMapping("/getrolesBypid")
    public Result getRolesBypid(){

        List<ProjectroleInfo> list = projectRoleService.getRolesByPid();
        if(null == list) return Result.fail(500,"查询失败");
        ArrayList<ProjectroleInfo> objects = new ArrayList<>();
        for (ProjectroleInfo projectroleInfo : list) {
            if(projectroleInfo.getProjectRoleState().equals("有效")){
                objects.add(projectroleInfo);
            }
        }
        return Result.success(200,"查询成功",objects);
    }

    /**
     * 查找所有项目角色
     * @return
     */
    @ApiOperation("查找所有项目角色")
    @PostMapping("/getroles")
    public Result getRoleBypid(){
        List<ProjectroleInfo> list = projectRoleService.getRoles();
        if(null == list) return Result.fail(500,"查询失败");
        return Result.success(200,"查询成功",list);
    }

    /**
     * 修改项目角色信息
     * @param projectNewroleForm 项目信息
     * @return
     * @throws Exception
     */
    @ApiOperation("修改项目角色信息")
    @PostMapping("/changeprojectrole")
    public Result changeProjectrole(@RequestBody ProjectNewroleForm projectNewroleForm) throws Exception {
        System.out.println(projectNewroleForm);

        boolean b = projectRoleService.changeroles(projectNewroleForm);
        if(!b) return Result.fail(500,"修改失败");
        return Result.success(200,"修改成功",null);
    }

    /**
     * 禁用/启用项目角色
     * @param projectroleStateForm 项目角色状态
     * @return
     */
    @ApiOperation("禁用/启用")
    @PostMapping("/changerolestate")
    public Result changeRolestate(@RequestBody ProjectroleStateForm projectroleStateForm){
        boolean b = projectRoleService.changestate(projectroleStateForm);
        if(!b) return Result.fail(500,"操作失败");
        return Result.success(200,"操作成功",null);
    }

    /**
     * 删除项目角色
     * @param projectroleStateForm 项目角色状态
     * @return
     */
    @ApiOperation("删除项目角色")
    @PostMapping("/removerole")
    public Result removeRole(@RequestBody ProjectroleStateForm projectroleStateForm){
        System.out.println(projectroleStateForm);

        QueryWrapper<ProjectRole> wrapper = new QueryWrapper<>();
        wrapper.eq("project_role_id",projectroleStateForm.getProjectRoleId());

        boolean remove = projectRoleService.remove(wrapper);
        if(!remove) return Result.fail(500,"删除失败");
        return Result.success(200,"删除成功",null);
    }

    /**
     * 批量删除选中项目角色
     * @param list 选中项目角色的id
     * @return
     */
    @ApiOperation("删除选中项目角色")
    @PostMapping("/removesome")
    public Result removeSomeRoles(@RequestBody List<Integer> list){
        System.out.println(list);

        boolean b = projectRoleService.removesome(list);
        if(!b) return Result.fail(500,"删除失败");
        return Result.success(200,"删除成功",null);
    }

    /**
     * 查询系统中身份是管理者的项目角色
     * @return
     */
    @ApiOperation("查询系统中身份是管理者的项目角色")
    @PostMapping("/getManagerRole")
    public Result getManagerRole(){
        List<ProjectroleInfo> projectroleInfos=projectRoleService.getManagerRole();
        return Result.success(200,"查询成功",projectroleInfos);
    }
}

